package cap.dao.impl;

import cap.bean.User;
import cap.dao.UserDao;
import cap.db.DBPool;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    Connection conn=null;
    PreparedStatement ps=null;
    ResultSet rs=null;
    /**
     *登录
     */
    @Override
    public User login(String userName, String password) {
        User u = null;
        try {
            u = new User();
            String sql="select * from muser where username=? and password=?";
            conn=DBPool.getConn();
            ps=conn.prepareStatement(sql);
            ps.setString(1,userName);
            ps.setString(2,password);
            rs=ps.executeQuery();
            if (rs.next()) {
                u.setId(rs.getInt("id"));
                u.setEmail(rs.getString("email"));
                u.setUserName(rs.getString("username"));
                u.setIsApplied(rs.getInt("is_applied"));
                u.setIsDelete(rs.getInt("is_delete"));
                u.setIsProfile(rs.getInt("is_profile"));

            } else {
                u = null;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBPool.close(rs,ps,conn);
        }

        return u;
    }
    /**
     *注册
     */
    @Override
    public int register(String email, String username, String password) {
        User u = null;
        int res = -1;
        try {
            u = new User();
            String sql="insert into muser (username,password,email,is_applied,is_delete,is_profile) values(?, ?, ?, 0, 0, 0)";
            conn=DBPool.getConn();
            ps=conn.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            ps.setString(3,email);
            res=ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBPool.close(rs,ps,conn);
        }
        return res;
    }

    /**
     *根据用户名获取id
     */
    @Override
    public int getIdByuserName(String username) {
        int userid = 0;
        try {
            String sql="select id from muser where username=?";
            conn=DBPool.getConn();
            ps=conn.prepareStatement(sql);
            ps.setString(1,username);
            rs=ps.executeQuery();
            if (rs.next()) {
                userid = rs.getInt("id");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBPool.close(rs,ps,conn);
        }

        return userid;
    }
    /**
     *获取所有用户
     */
    @Override
    public List<User> getAllUser() {
        List<User> uList = null;
        try {
            String sql="select * from muser";
            uList = new ArrayList<User>();
            conn=DBPool.getConn();
            ps=conn.prepareStatement(sql);
            rs=ps.executeQuery();
            while (rs.next()) {
                User u = new User();
                u.setId(rs.getInt("id"));
                u.setEmail(rs.getString("email"));
                u.setUserName(rs.getString("username"));
                u.setIsApplied(rs.getInt("is_applied"));
                u.setIsDelete(rs.getInt("is_delete"));
                u.setIsProfile(rs.getInt("is_profile"));

                uList.add(u);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBPool.close(rs,ps,conn);
        }
        return uList;
    }
    /**
     *根据id获取用户
     */
    @Override
    public User getUserById(int id) {
        User u = null;
        try {
            u = new User();
            String sql="select * from muser where id=?";
            conn=DBPool.getConn();
            ps=conn.prepareStatement(sql);
            ps.setInt(1,id);
            rs=ps.executeQuery();
            while (rs.next()) {
                u.setId(rs.getInt("id"));
                u.setEmail(rs.getString("email"));
                u.setUserName(rs.getString("username"));
                u.setIsApplied(rs.getInt("is_applied"));
                u.setIsDelete(rs.getInt("is_delete"));
                u.setIsProfile(rs.getInt("is_profile"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBPool.close(rs,ps,conn);
        }
        return u;
    }
    /**
     *根据id激活用户
     */
    @Override
    public int setIsAppliedById(int id) {
        int res = -1;
        try {
            String sql="update muser set is_applied=1 where id=?";
            conn=DBPool.getConn();
            ps=conn.prepareStatement(sql);
            ps.setInt(1,id);
            res=ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBPool.close(rs,ps,conn);
        }
        return res;
    }
    /**
     *根据id和密码获取用户
     */
    @Override
    public User getByIdPwd(int userId, String password) {
        User u = null;
        try {
            String sql="select * from muser where id=? and password=?";
            conn=DBPool.getConn();
            ps=conn.prepareStatement(sql);
            ps.setInt(1,userId);
            ps.setString(2,password);
            rs=ps.executeQuery();
            while (rs.next()) {
                u = new User();
                u.setId(rs.getInt("id"));
                u.setEmail(rs.getString("email"));
                u.setUserName(rs.getString("username"));
                u.setIsApplied(rs.getInt("is_applied"));
                u.setIsDelete(rs.getInt("is_delete"));
                u.setIsProfile(rs.getInt("is_profile"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBPool.close(rs,ps,conn);
        }
        return u;
    }
    /**
     *根据id修改密码
     */
    @Override
    public int updatePwdById(int userId, String password) {
        int res = -1;
        try {
            String sql="update muser set password=? where id=?";
            conn=DBPool.getConn();
            ps=conn.prepareStatement(sql);
            ps.setString(1,password);
            ps.setInt(2,userId);
            res=ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBPool.close(rs,ps,conn);
        }
        return res;
    }
    /**
     *根据邮箱获取用户
     */
    @Override
    public User getByEmail(String email) {
        User u = null;
        try {
            String sql="select * from muser where email=?";
            conn=DBPool.getConn();
            ps=conn.prepareStatement(sql);
            ps.setString(1,email);
            rs=ps.executeQuery();
            while (rs.next()) {
                u = new User();
                u.setId(rs.getInt("id"));
                u.setEmail(rs.getString("email"));
                u.setUserName(rs.getString("username"));
                u.setIsApplied(rs.getInt("is_applied"));
                u.setIsDelete(rs.getInt("is_delete"));
                u.setIsProfile(rs.getInt("is_profile"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBPool.close(rs,ps,conn);
        }
        return u;
    }
    /**
     *根据id删除
     */
    @Override
    public int deleteUser(int uId){
        int res = -1;
        try {
            String sql="update muser set is_delete=1 where id=?";
            conn=DBPool.getConn();
            ps=conn.prepareStatement(sql);
            ps.setInt(1,uId);
            res=ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBPool.close(rs,ps,conn);
        }
        return res;
    }
    /**
     *根据id修改活跃度
     */
    @Override
    public int activeUser(int uId){
        int res = -1;
        try{
            String sql="update muser set is_delete=0 where id=?";
            conn=DBPool.getConn();
            ps=conn.prepareStatement(sql);
            ps.setInt(1,uId);
            res=ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally{
            DBPool.close(rs,ps,conn);
        }
        return res;
    }
    /**
     *设置用户信息是否完善
     */
    @Override
    public int setIsProfile(int userId) {
        int res = -1;
        try {
            String sql="update muser set is_profile=1 where id=?";
            conn=DBPool.getConn();
            ps=conn.prepareStatement(sql);
            ps.setInt(1,userId);
            res=ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally{
            DBPool.close(rs,ps,conn);
        }
        return res;
    }
    /**
     *分页显示用户
     */
    @Override
    public List<User> getUserByPage(int curPage, int size) {
        List<User> uList = null;
        try {
            String sql="select * from muser limit ?,?";
            uList = new ArrayList<User>();
            conn=DBPool.getConn();
            ps=conn.prepareStatement(sql);
            ps.setInt(1,curPage);
            ps.setInt(2,size);
            rs=ps.executeQuery();
            while (rs.next()) {
                User u = new User();
                u.setId(rs.getInt("id"));
                u.setEmail(rs.getString("email"));
                u.setUserName(rs.getString("username"));
                u.setIsApplied(rs.getInt("is_applied"));
                u.setIsDelete(rs.getInt("is_delete"));
                u.setIsProfile(rs.getInt("is_profile"));
                uList.add(u);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBPool.close(rs,ps,conn);
        }
        return uList;
    }
}
